//
//  AddressViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/16.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "AddressViewController.h"
#import "AddAddressViewController.h"
#import "AddressCell.h"

@interface AddressViewController () <UITableViewDataSource , UITableViewDelegate>

@property (nonatomic, strong) NSString *checkIndex;
@property (nonatomic, strong) NSArray *addressArray;

@end

static NSString *addressCellIdentifier = @"AddressCell";

@implementation AddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"AddressCell" bundle:nil]
         forCellReuseIdentifier:addressCellIdentifier];
    self.title = self.addressTitle;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"新增" style:UIBarButtonItemStyleBordered target:self action:@selector(addNewAddressButton)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIUtil colorWithHexString:appFontColor]}];
    self.checkIndex = self.addressIndex;
    [self requestData];
}

- (void)requestData {
     NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken};
    [RequestData queryAddressList:[UIUtil toJsonString:dic] withBlock:^(NSArray *array, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.addressArray = array;
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - action methods
- (void)addNewAddressButton {
    AddAddressViewController *view = [[AddAddressViewController alloc] init];
    view.titleName = @"新增收货地址";
    view.status = @"0";
    [self.navigationController pushViewController:view animated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.addressArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:addressCellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[AddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addressCellIdentifier];
    }
//    if (indexPath.row == 0) {
//        cell.ifDefault = YES;
//    }
    cell.addressModel = [self.addressArray objectAtIndex:indexPath.row];
    if ([[NSString stringWithFormat:@"%ld", (long)indexPath.row] isEqualToString:self.checkIndex]) {
        cell.ifCheck = YES;
    } else {
        cell.ifCheck = NO;
    }
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.ifModify) {
        AddAddressViewController *view = [[AddAddressViewController alloc] init];
        view.titleName = @"编辑收货地址";
        view.status = @"2";
        view.addressModel = [self.addressArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:view animated:YES];
    } else {
        self.checkIndex = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        if (_chooseAddressBlock) {
            _chooseAddressBlock([self.addressArray objectAtIndex:indexPath.row],[NSString stringWithFormat:@"%ld",(long)indexPath.row]);
        }
        [self.tableView reloadData];
    }
}

@end
